"""
通用数据模型类

"""

from peewee import CharField, TextField, ForeignKeyField
from models.base_model import BaseModel
from models.user import Organization, User


class File(BaseModel):
    TYPE_MAP = (
        ('fault_picture', '故障单图片'),
        ('bill_file', '账单文件'),
    )
    type = CharField(max_length=200, verbose_name='文件所属类型', choices=TYPE_MAP, default='fault_picture')
    file = CharField(null=True, verbose_name='文件')

    class Meta:
        db_table = 'xingxiu_file'


class NoRule(BaseModel):
    model_name = CharField(max_length=100, verbose_name='数据表model名称')  # 数据表的model名
    # 编号规则 以 RQ220708A0001 为例
    # RQ+%time:Ymd%+%filed-type:port=A,p2p=B%+%auto:4%
    # RQ为前缀 %%之间的是规则公式
    # time:Ymd代表 年月日
    # filed-type:port=A,p2p=B 代表根据表中的type字段进行区分，如果type字段为port则为A，type为p2p则为B
    # auto: 4 代表自增 位数为4
    rule = TextField(verbose_name='规则')
    note = TextField(null=True, verbose_name='备注')

    class Meta:
        db_table = 'xingxiu_no_rule'


class EmailLog(BaseModel):
    TYPE_MAP = (
        ('fault_email', '故障单邮件'),
        ('bill_email', '账单邮件'),
    )
    type = CharField(max_length=200, verbose_name='邮件所属类型', choices=TYPE_MAP, default='bill_email')
    start_org = ForeignKeyField(Organization, on_delete='CASCADE', verbose_name='邮件发起方',
                                backref='email_start', null=True)
    start_mem = ForeignKeyField(User, on_delete='CASCADE', verbose_name='邮件发起方所属人',
                                backref='email_start_mem', null=True)
    receive_org = ForeignKeyField(Organization, on_delete='CASCADE', verbose_name='邮件接收方',
                                  backref='email_receive', null=True)
    receive_mem = CharField(max_length=255, verbose_name='邮件接收方所属人', null=True)
    title = CharField(max_length=64, verbose_name='邮件标题', null=True)
    content = TextField(null=True, verbose_name='邮件内容')

    class Meta:
        db_table = 'xingxiu_email_log'


class EmailConfig(BaseModel):
    email_host = CharField(max_length=255, verbose_name='邮件ip', null=True)
    email_host_user = CharField(max_length=255, verbose_name='邮件用户', null=True)
    email_host_password = CharField(max_length=255, verbose_name='邮件密码', null=True)
    org = ForeignKeyField(Organization, verbose_name='组织机构', on_delete='CASCADE', null=True,
                          backref='email_relation')

    class Meta:
        db_table = 'xingxiu_email_config'
